package com.e.dianshang.controller;

import com.e.dianshang.pojo.User;
import com.e.dianshang.service.UserService;
import com.e.dianshang.utils.BusinessException;
import com.e.dianshang.utils.JsonResult;
import com.e.dianshang.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description: TOOD
 * @ClassName: UserController
 * @Author: ${陈柯宇}
 * Date: 2020/11/11 14:56
 * @Version: 1.0
 */
@RestController
@Api(value = "登录注册接口",tags = "登录注册")
public class UserController {

    @Resource(name = "userService")
    UserService userService;


    //java要操作redis,是借助于RedisTemplate
    @Resource
    RedisTemplate<String,Object> redisTemplate;

    /**
     * 查找手机号是否存在
     * @param phone
     * @return
     */

    @PostMapping(value = "/findByPhone",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查看手机号是否存在的接口")
    @ApiImplicitParam(name = "phone",value = "当前用户电话号码",required = true,
            dataType = "String",paramType = "query")
    public Map findByPhone(@RequestParam("phone") String phone){
        JsonResult jsonResult=new JsonResult();
        String byPhone = userService.findByPhone(phone);
        if(byPhone!=null && !byPhone.equals("")){
            jsonResult.setCode("0");
            jsonResult.setMsg("该手机已经注册！！！");
        }else {
            jsonResult.setCode("1");
            jsonResult.setMsg("可以注册");
        }
        System.out.println(byPhone);
        return jsonResult.getValues();
    }


    /**
     * 注册接口
     * @param name
     * @param pwd
     * @param phone
     * @param local
     * @param
     * @return
     * @throws BusinessException
     */
    @PostMapping(value = "/reg",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "注册的接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "name",value = "姓名",required = false, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "phone",value = "当前用户电话号码",required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "local",value = "用户所在地",required = false, dataType = "String",paramType = "query"),})
    public Map reg(@RequestParam("name") String name,@RequestParam("pwd") String pwd,@RequestParam("phone") String phone,@RequestParam("local") String local) {
        JsonResult jsonResult=new JsonResult();
//        //判断验证码是否合法
//        //合法，将个人信息+注册时间  添加到数据库
//        //不合法，返回注册失败：验证码不正确
//        String byPhone = userService.findByPhone(phone);
//        //1.利用RedisTemplate绑定要操作的数据类型,生成对应的操作符对象
//        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
//        //2.通过key访问value
//        Object s=  operations.get(phone);
////        BoundValueOperations<String, Object> operations1 = redisTemplate.boundValueOps("sms:" + phone);
////        operations1.get();
        User user=new User();
        user.setName(name);
        user.setPwd(MD5Util.encode(pwd));
        user.setPhone(phone);
        user.setLocal(local);
        Integer line = userService.register(user);
        if (line>0) {
            jsonResult.setCode("1");
            jsonResult.setMsg("注册成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("注册失败，请重试！");
        }
        return jsonResult.getValues();
    }



    @PostMapping(value = "/same",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "验证验证码是否正确")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone",value = "当前用户电话号码",required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "captcha",value = "验证码",required = true, dataType = "String",paramType = "query")})
    public Map same(@RequestParam("phone") String phone,@RequestParam("captcha") String captcha){
        JsonResult jsonResult=new JsonResult();
        //判断验证码是否合法
        //合法，将个人信息+注册时间  添加到数据库
        //不合法，返回注册失败：验证码不正确
        String byPhone = userService.findByPhone(phone);
        //1.利用RedisTemplate绑定要操作的数据类型,生成对应的操作符对象
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        //2.通过key访问value
//        Object s=  operations.get(phone);
        BoundValueOperations<String, Object> operations1 = redisTemplate.boundValueOps("ds:" + phone);
        String s = (String) operations1.get();
        if (byPhone!=null){
            jsonResult.setCode("0");
            jsonResult.setMsg("该账号已经注册！");
        }
        if (captcha==null) {
            jsonResult.setCode("0");
            jsonResult.setMsg("验证码输入为空！");
        }
        if (!captcha.equals(s)) {
            jsonResult.setCode("0");
            jsonResult.setMsg("验证码输入不正确！");
        }
        return jsonResult.getValues();
    }


    @PostMapping(value = "/getsame",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "验证验证码是否正确")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone",value = "当前用户电话号码",required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "captcha",value = "验证码",required = true, dataType = "String",paramType = "query")})
    public Map getsame(@RequestParam("phone") String phone,@RequestParam("captcha") String captcha){
        JsonResult jsonResult=new JsonResult();
        BoundValueOperations<String, Object> operations1 = redisTemplate.boundValueOps("get:" + phone);
        String s = (String) operations1.get();
        if (captcha==null || !captcha.equals(s)) {
            jsonResult.setCode("0");
            jsonResult.setMsg("验证码输入不正确！");
        }
        return jsonResult.getValues();
    }


    /**
     * 登录接口
     * @param phone
     * @param pwd
     * @return
     * @throws BusinessException
     */
    @ApiOperation(value = "用户登录的接口",notes = "用户登录",httpMethod = "POST",response = String.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "phone",value = "当前用户电话号码",required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "pwd",value = "当前用户密码",required = true, dataType = "String",paramType = "query")})
    @RequestMapping(value = "/login",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map login(@RequestParam("phone")String phone,@RequestParam("pwd")String pwd) throws BusinessException {
        JsonResult jsonResult=new JsonResult();
        String byPhone = userService.findByPhone(phone);
        String userPwd1= userService.findPwdByPhone(phone);
        if (phone==null || phone.trim().equals("")) {
            throw new BusinessException("手机号码输入为空，请输入！");
        }
        if (byPhone==null){
            jsonResult.setMsg("0");
            jsonResult.setMsg("该手机号尚未注册，请前往注册！");
        }
        String userPwd2 = MD5Util.encode(pwd);
        if (userPwd1.equals(userPwd2)) {
            jsonResult.setCode("1");
            jsonResult.setMsg("登录成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("账号或密码错误！");
            //throw new BusinessException("该手机号码未注册，请注册！");
        }
        return jsonResult.getValues();
    }

    @ApiOperation(value = "查询用户名",notes = "测试",httpMethod = "POST",response = String.class)
    @ApiImplicitParam(name = "phone",value = "当前用户电话号码",required = true, dataType = "String",paramType = "query")
    @RequestMapping(value = "/findName",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map findName(@RequestParam("phone")String phone)  {
        JsonResult jsonResult=new JsonResult();
        String name = userService.findNameByPhone(phone);
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(name);
        return jsonResult.getValues();
    }



    @ApiOperation(value = "查修密码是否相同",notes = "测试",httpMethod = "POST",response = String.class)
    @RequestMapping(value = "/findPwd",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map findPwd(@RequestParam("phone")String phone,@RequestParam("pwd")String pwd) {
        JsonResult jsonResult=new JsonResult();
        String userPwd1= userService.findPwdByPhone(phone);
        String userPwd2 = MD5Util.encode(pwd);
        if (userPwd1.equals(userPwd2)) {
            jsonResult.setCode("0");
            jsonResult.setMsg("新密码不得与原密码相同！");
        }else {
            jsonResult.setCode("1");
            jsonResult.setMsg("请确认密码！");
        }
        return jsonResult.getValues();
    }


    @ApiOperation(value = "找回密码的接口",notes = "找回同时修改密码",httpMethod = "POST",response = String.class)
    @ApiImplicitParam(name = "phone",value = "当前用户电话号码",required = true, dataType = "String",paramType = "query")
    @RequestMapping(value = "/updatePwd",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map updatePwd(@RequestParam("phone")String phone,@RequestParam("pwd")String pwd){
        JsonResult jsonResult=new JsonResult();
        String userPwd= MD5Util.encode(pwd);
        Integer integer = userService.updatePwd(phone, userPwd);
        if (integer>0) {
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("系统开小差了，请稍后再试！");
        }
        return jsonResult.getValues();
    }



}
